package logic

import (
	"context"
	"errors"
	"mooc_srv/models"

	"mooc_srv/internal/svc"
	"mooc_srv/mooc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品发布
func (l *GoodsAddLogic) GoodsAdd(in *mooc.GoodsAddRequest) (*mooc.GoodsAddResponse, error) {
	// todo: add your logic here and delete this line

	var err error

	g := models.Goods{
		GoodsTitle: in.GoodsTitle,
		GoodsPrice: float64(in.GoodsPrice),
		GoodsNum:   int(in.GoodsNum),
	}

	err = g.Create()
	if err != nil {
		logx.Error("商品发布失败")
		return nil, errors.New("商品发布失败")
	}

	return &mooc.GoodsAddResponse{
		GoodsId: int64(g.ID),
	}, nil
}
